package hv.fmw.core.command 
{
	import flash.events.EventDispatcher;
	
	/**
	 * ...
	 * @author hvong
	 */
	public class SequentialCommand extends EventDispatcher
	{
		private var _commands:Array = [];
		private var _index:int = 0;
		
		public function SequentialCommand () 
		{
			
		}
		
		public function addCommand(command:Command):void {
			_commands.push(command);
		}
		
		public function execute():void {
			var command:Command = _commands[_index];
				command.addEventListener(CommandEvent.COMMAND_COMPLETE, _onCommandComplete);
				ICommand(command).execute();
		}
		
		private function _onCommandComplete(event:CommandEvent):void {
			dispatchEvent(new CommandEvent(CommandEvent.COMMAND_COMPLETE));
			
			_index++;
			if(_index<_commands.length){
				execute();
			} else {
				dispatchEvent(new CommandEvent(CommandEvent.ALL_COMMAND_COMPLETE));
			}
		}
	}
}